package com.itheima;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 索引库的创建
 * @作者 itcast
 * @创建日期 2020/11/3 9:00
 **/
public class EsDemo01 {
    RestHighLevelClient client;

    @Test
    public void createIndex(){
//        1. 创建一个 创建索引库的请求  对象
        CreateIndexRequest request = new CreateIndexRequest("user");
//        1.1 设置索引库名称
//        1.2 设置settings
//        Map map = new HashMap();
//        map.put("number_of_shards",1);
        request.settings("{\n" +
                "    \"number_of_shards\": 1,  \n" +
                "    \"number_of_replicas\": 1\n" +
                "  }", XContentType.JSON);
//        1.3 设置mapping
        request.mapping("{\n" +
                "    \"properties\": {\n" +
                "      \"id\": {\n" +
                "        \"type\": \"long\"\n" +
                "      },\n" +
                "      \"name\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"age\":{\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"gender\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"note\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }\n" +
                "  }",XContentType.JSON);
//        2. 执行请求 使用客户端指定对应操作的请求
        try {
            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
//        3. 处理返回结果
            System.out.println(createIndexResponse.isAcknowledged());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void deleteIndex() throws IOException {
        //1 创建  删除index的请求
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        //2. 执行请求
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        //3. 解析结果
        System.out.println(delete.isAcknowledged());
    }





    @Before
    public void init(){
        // 构建ES的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost",9200,"http")
                )
        );
        
        
        
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
